//
//  LBViewController.m
//  LBPageControl
//
//  Created by liuboliu on 06/10/2023.
//  Copyright (c) 2023 liuboliu. All rights reserved.
//

#import "LBViewController.h"
#import <LBHorizontalLoopView/LBHorizontalLoopView.h>
#import <LBPageControl/LBPageControl.h>
#import "LBLoopViewCell.h"
#import "UIColor-Expanded.h"

@interface LBViewController () <LBHorizontalLoopViewDelegate, LBHorizontalLoopViewDataSource>

@property (nonatomic, strong) LBHorizontalLoopView *loopView;

@property (nonatomic, strong) LBPageControl *pageControl;

@end

@implementation LBViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.loopView];
    [self.view addSubview:self.pageControl];
    [self.loopView reloadDataAndStartRoll];
    self.pageControl.pageCount = 5;
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - LBHorizontalLoopViewDelegate, LBHorizontalLoopViewDataSource

- (NSInteger)viewNumberOfLoopView:(LBHorizontalLoopView *)loopView
{
    return 5;
}

- (LBHorizontalLoopViewCell *)cellForIndex:(NSInteger)index inLoopView:(LBHorizontalLoopView *)loopView
{
    LBLoopViewCell *cell = [loopView dequeueReusableCellWithIdentifier:NSStringFromClass([LBLoopViewCell class])];
    [cell updateWithModel:[NSString stringWithFormat:@"%ld",index]];
    return cell;
}

- (void)loopView:(LBHorizontalLoopView *)loopView didScrollToCellAtIndex:(NSInteger)index
{
    [self.pageControl pageContolAnimationIndex:self.loopView.currentIndex];
}

- (void)loopView:(LBHorizontalLoopView *)loopView didScroll:(UIScrollView *)scrollView
{
    CGFloat scroW = scrollView.frame.size.width;
    ///滚动距离绝对值
    CGFloat marginX = fabs(scroW-scrollView.contentOffset.x);
    float scroProgress = marginX/scroW;
   // NSLog(@"哈哈哈进度值%f", scroProgress);
    bool left = NO;
    if ((scroW-scrollView.contentOffset.x)>0) {
        left = YES;
    } else {
        left = NO;
    }
    self.pageControl.isLeftScro = left;
    self.pageControl.scroProgress = scroProgress;
}

#pragma mark - lazy load

- (LBHorizontalLoopView *)loopView
{
    if (!_loopView) {
        _loopView = [[LBHorizontalLoopView alloc] initWithFrame:CGRectMake(0 , 100 , CGRectGetWidth(self.view.frame), 120 )];
        [_loopView registerClass:[LBLoopViewCell class] forViewReuseIdentifier:NSStringFromClass([LBLoopViewCell class])];
        _loopView.backgroundColor = [UIColor cyanColor];
//        _loopView.restartTimeEndingDragging = NO;
//        _loopView.style = TPHorizontalLoopViewStyleAlphaChange;
        _loopView.delegate = self;
        _loopView.dataSource = self;
    }
    return _loopView;
}

- (LBPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [[LBPageControl alloc] initWithFrame:CGRectMake(160, 300, 80, 30)];
        _pageControl.selectedItemColor = [UIColor colorWithHexString:@"0x999999"];
        _pageControl.normalItemColor = [UIColor colorWithHexString:@"0xcccccc"];
     }
    return _pageControl;
}

@end
